<?php
namespace Home\Controller;
use Think\Controller;
class ClassmanageController extends AllowController {

    // 班级列表
    public function index(){
        // 实例化数据表
        $class = M('class');
        $user = M('users');
        $major = M('major');
        $classroom = M('class_room');
        // 创建数组存放数据
        $arr = array();
        $te = array();
        // 存储查询条件数组
        $where = array();
        $search = "";
        $grd = array();
        // 班級名查询
        if($_GET['classname'] !== null){
            $where[] = "stu_class.name like '%{$_GET['classname']}%'";
            $search = "/classname/{$_GET['classname']}";
        }
        // 班主任姓名查询
        if(!empty($_GET['admin'])){
            $where[] = "admin_id = {$_GET['admin']}";
            $search .= "/admin_id/{$_GET['admin']}";
        }
        // 拼接查询语句
        if(count($where)>0){
            $grd = implode(' and ',$where);

        }else{
            $where = "";
        } 
        //判断是否使用了按班级名或班主任查找
        if(!empty($_GET['classname']) || !empty($_GET['admin'])){
            // 获取班级总数
            $tot = $class->where($where)->count();
        }else{
            // 获取班级总数
            $tot = $class->count();
        }
        // 分页
        $nums = 5;
        $page = new \Think\Page($tot,$nums);
        // 自定义常量
        $i=0;
        // 遍历出所需要的字段信息
        $list = $class->limit($page->firstRow,$page->listRows)->where($grd)->field('stu_class.id,stu_class.name,stu_users.name as admin,stu_class.teacher_id as teacher,stu_class.pm_id as pm,stu_class.start_time,stu_class.end_time,stu_class.major_id,stu_class.status,stu_class.class_room')->order("id asc")->join('stu_users on stu_users.id=stu_class.admin_id')->select();
        // 把遍历出数据放到$arr数组中
        foreach ($list as $key => $value) {
            $arr[$i]['id'] = $value['id'];
            $arr[$i]['name'] = $value['name'];
            $arr[$i]['admin'] = $value['admin'];
            $arr[$i]['teacher'] = $value['teacher_id'];
            $arr[$i]['pm'] = $value['pm_id'];
            $arr[$i]['start'] = date('Y-m-d',$value['start_time']);
            $arr[$i]['end'] = date('Y-m-d',$value['end_time']);
            $arr[$i]['major'] = $value['major_id'];
            $arr[$i]['status'] = $value['status'];
            $arr[$i]['class_room'] = $value['class_room'];
            $i+=1;
        }
        // 专业
        $list1 = $class->limit($page->firstRow,$page->listRows)->where($grd)->field('stu_major.name')->join('stu_major on stu_major.id=stu_class.major_id')->select();
        $i=0;
        foreach ($list1 as $key => $value) {
            $te[$i]['major'] = $value['name'];
            $i += 1;
        }
        // 把$te数据存储到$arr数组中
        for($j=0;$j<$i;$j++){
            $arr[$j]['major'] = $te[$j]['major'];
        } 
        // 教室
        $list1 = $class->limit($page->firstRow,$page->listRows)->where($grd)->field('stu_class_room.name')->join('stu_class_room on stu_class_room.id=stu_class.class_room')->select();

        // var_dump($list1);
        $i=0;
        foreach ($list1 as $key => $value) {
            $te[$i]['class_room'] = $value['name'];
            $i += 1;
        }
        // 把$te数据存储到$arr数组中
        for($j = 0;$j<$i;$j++){
            $arr[$j]['class_room'] = $te[$j]['class_room'];
        } 
        // 查询讲师
        $i=0;
        $list2=$class->limit($page->firstRow,$page->listRows)->where($grd)->field('stu_class.id,stu_users.name as teacher')->join("stu_users on stu_class.teacher_id=stu_users.id")->select();
        // 把遍历出的讲师数据存储到$te数组中
        foreach ($list2 as $key => $value) {
             $te[$i]['teacher'] = $value['teacher'];
             $te[$i]['id'] = $value['id'];
            $i += 1;
        }
        // 把$te数据存储到$arr数组中
        for($j = 0;$j<$i;$j++){
            $arr[$j]['teacher']=$te[$j]['teacher'];
            $arr[$j]['id']=$te[$j]['id'];
        } 
        // 查询项目经理
        $i = 0;
        $list2 = $class->limit($page->firstRow,$page->listRows)->where($grd)->field('stu_users.name as pm')->join("stu_users on stu_class.pm_id=stu_users.id")->select();
        // 把遍历出的项目经理数据存储到$te数组中
        foreach ($list2 as $key => $value) {
            $te[$i]['pm']=$value['pm'];
            $i += 1;
        }
        // 把$te数据存储到$arr数组中
        for($j = 0;$j<$i;$j++){
            $arr[$j]['pm']=$te[$j]['pm'];
        } 
        // 遍历出所有班主任
        $admins = $user->where('role_id=5')->field('id,name')->select();
        $this->assign('admins',$admins);
        $this->assign('list',$arr);
        // 分配分页信息
        // 接受当前页,默认打开为1
        if (empty($_GET['p'])) {
            $p = "1";
        }else{
            $p = $_GET['p'];
        }
        $this->assign("curr",$p);// 分配当前页的页数
        $this->assign('total',$tot);// 总条数
        $this->assign('page',ceil($tot/$nums));// 分页的页数
        $this->assign('search',$search);
        $this->display('ClassManage/index');        
    }

    // 遍历信息
    public function select(){
        $class = M('class');
        $user = M('users');
        $major = M('major');
        $classroom = M('class_room');
        $admin = array();//创建数组存储班主任数据
        $teacher = array();//创建数组存储讲师数据 
        $pm = array();//创建数组存储项目经理数据 
        $maj = array();//创建数组存储专业数据
        // 遍历出班主任数据
        $list1 = $user->field('id,name')->where("role_id=5")->select();
        foreach ($list1 as $key => $value) {
            $admin[] = ['id'=>$value['id'],'name'=>$value['name']];
        }
        // 遍历出讲师数据
        $list1 = $user->field('id,name')->where("role_id=6")->select();
        foreach ($list1 as $key => $value) {
            $teacher[] = ['name'=>$value['name'],'id'=>$value['id']];
        }
        // 遍历出项目经理数据
        $list1 = $user->field('id,name')->where("role_id=7")->select();
        foreach ($list1 as $key => $value) {
            $pm[] = ['name'=>$value['name'],'id'=>$value['id']];
        }
        // 遍历出专业数据
        $list1=$major->field('id,name')->select();
        foreach ($list1 as $key => $value) {
            $maj[] = ['id'=>$value['id'],'name'=>$value['name']];
        }
        // 遍历出还没有被使用教室数据
        $info = $class->field("class_room")->where("status in (0,1)")->select();
        $rooms = array();
        foreach ($info as $value) {
            $rooms[] = $value['class_room'];
        }
        $roomm = implode(',',$rooms);
        // var_dump($roomm);
        if($roomm==""){
            $list2 = $classroom->field('id,name')->select();
        }else{
        $list2 = $classroom->field('id,name')->where("id not in ($roomm)")->select();

        }
        // $list2 = $classroom->field('id,name')->select();
        // var_dump($list2);

        // 班级状态
        $list1 = $class->field('status')->select();
        $this->assign('list',$list1);   
        $this->assign('admin',$admin);// 分配班主任数据 
        $this->assign('teacher',$teacher);// 分配讲师数据
        $this->assign('pm',$pm);// 分配项目经理数据
        $this->assign('major',$maj);// 分配专业数据
        $this->assign('roomm',$list2); // 分配add.html教室数据
    }

    // 添加班级
    public function add(){
        // 调用select方法
        $this->select();       
        $this->display("ClassManage/add");
    }

    // 执行添加  
    public function doadd(){
        $class = M('class');
        // 创建数组存储获取数据
        $data = array(); 
        // 获取页面传递数据
        $data['name'] = I('post.classname');
        $data['admin_id'] = I('post.admin');
        $data['teacher_id'] = I('post.teacher');
        $data['pm_id'] = I('post.pm');
        $data['start_time'] = strtotime(I('post.start'));
        $data['end_time'] = strtotime(I('post.end'));
        $data['major_id'] = I('post.major');
        $data['status'] = I('post.status');
        $data['class_room'] = I('post.classroom');
        $data['created'] = time();
        $list = $class->add($data);
        if($list){
            echo "添加成功";
        }else{
            echo "0";
        }
    }

    // 删除
    // public function delete(){
    //         $arr = array("id"=>I("post.id"));
    //         $mod = M("class");
    //         $info = $mod->where($arr)->delete();
    //         // echo $mod->getLastSql();
    //         if($info){
    //             echo "成功";
    //         }else{
    //             echo "失败";
    //         }
    //     }

    // 修改
    public function update(){
        $classroom = M('class_room');
        $class = M('class');
        // 调用自定方法
        $this->select(); 
        $id = I('get.id');
        $dd = $class->where("id=$id")->select(); 
        // 格式化时间
        $dd[0]['start_time'] = date('Y-m-d',$dd[0]['start_time']);
        $dd[0]['end_time'] = date('Y-m-d',$dd[0]['end_time']);
        // 遍历没有使用教室数据
        $info = $class->field("class_room")->where("status in (0,1)")->select();
        $rooms = array();
        foreach ($info as $value) {
            $rooms[] = $value['class_room'];
        }
        $roomm = implode(',',$rooms);
        $list2 = $classroom->field('id,name')->where("id not in ($roomm)")->select();
        $roomm = array();
        $i=0;
        foreach ($list2 as $key=>$value) {
            $roomm[$i]['id'] = $value['id'];
            $roomm[$i]['name'] = $value['name'];
            $i++;
        }
        // var_dump($roomm);
        // 获取本班级教室
        $list = $classroom->where("id={$dd[0][class_room]}")->select();
        foreach ($list as $key2 => $value) {
            $i++;
            $roomm[$i]['id'] = $value['id'];
            $roomm[$i]['name'] = $value['name'];
        }
        // var_dump($roomm);
        if ($dd['0']['status'] == 2) {
            $disabled = "disabled";
        }else{
            $disabled = "";
        }
        $this->assign('disabled',$disabled);
        $this->assign('rooms',$roomm); // 分配edit.html教室数据
        $this->assign('good',$dd);
        $this->display("ClassManage/edit");
    }

    public function doupdate(){
        $class = M('class');
        // 创建数组存储获取数据
        $data = array(); 
        // 获取页面传递数据
        $id=I('post.id');
        $data['name'] = I('post.classname');
        $data['admin_id'] = I('post.admin');
        $data['teacher_id'] = I('post.teacher');
        $data['pm_id'] = I('post.pm');
        $data['start_time'] = strtotime(I('post.start'));
        $data['end_time'] = strtotime(I('post.end'));
        $data['major_id'] = I('post.major');
        $data['status'] = I('post.status');
        $data['class_room'] = I('post.classroom');
        $data['created'] = time();
        // var_dump($data);
        $list = $class->where("id=$id")->save($data);
        if($list){
            echo "修改成功";
        }else{
            echo "0";
        }
    }

    // 遍历班级信息
    public function fff(){
        // 创建数组
        $arr = array();
        $id = I('get.id');
        $mod = M('class');
        // 班级信息
        $list = $mod->where("id=$id")->select();
        foreach ($list as $key => $value){
            $arr['id'] = $value['id'];
            $arr['name'] = $value['name'];
            $arr['admin'] = $value['admin_id'];
            $arr['teacher'] = $value['teacher_id'];
            $arr['pm'] = $value['pm_id'];
            $arr['start'] = date('Y-m-d',$value['start_time']);
            $arr['major'] = $value['major_id'];
            $arr['status'] = $value['status'];
            $arr['classroom'] = $value['class_room'];
        }
        //获取班主任信息
        $list2 = M('users')->field('stu_users.name as admin')->where("id={$arr['admin']}")->select();
        $arr['admin'] = $list2[0]['admin'];
        // 获取讲师信息
        $list2 = M('users')->field('stu_users.name as teacher')->where("id={$arr['teacher']}")->select();
        $arr['teacher'] = $list2[0]['teacher'];
        // 获取项目经理信息
        $list2 = M('users')->field('stu_users.name as pm')->where("id={$arr['pm']}")->select();
        $arr['pm'] = $list2[0]['pm'];
        // 获取班长信息
        $list2 = M('users')->field('stu_users.name as monitorname')->where("class={$arr['id']} and role_id=8")->select();
        $arr['monitorname'] = $list2[0]['monitorname'];
        // 获取专业信息
        $list2 = M('major')->field('stu_major.name as major')->where("id={$arr['major']}")->select();
        $arr['major'] = $list2[0]['major'];
        // 获取教室信息
        $list2 = M('class_room')->field('stu_class_room.name as classroom')->where("id={$arr['classroom']}")->select();
        $arr['classroom'] = $list2[0]['classroom'];
        // 获取同一个班级所有人姓名
        $list = M('users')->where("class=$id and team=0")->select();
        // var_dump($list);
        $names = array();
        foreach ($list as $key => $value) {
           $names[$key]['uid'] = $value['id'];
           $names[$key]['name'] = $value['name'];
           $names[$key]['roleid'] = $value['role_id'];
        }
        // 获取同一班级所有不同组所有成员
        $team = D("team");
        //把所有的小组遍历出来
        $arr1 = array("class"=>$id);
        $teams = $team->where($arr1)->select();
        $listt = array();
        $list1 = M("users")->where($arr1)->select();
        $this->assign('teams',$teams);
        $this->assign('list1',$list1);
        $this->assign('arr',$arr);
        $this->assign('names',$names);
    }

    // 管理
    public function admin(){
        $this->fff();
        $this->display('ClassManage/team');
    }

     // 查看通讯录
    public function MailList(){
        $user = M('users');
        // 获取班级
        $id = I("get.id");
        $classname = M('class')->where("id=$id")->select();
        $list = $user->where("class=$id and role_id in(10,8,9)")->field('name,phone,qq,email')->select();
        $this->assign('classid',$id);
        $this->assign('class',$classname);
        $this->assign('list',$list);
        $this->display("Classmanage/MailList");
    }
    // 班级每个人详细信息
    public function preInfo(){
        // 班级id
        $class = I("get.id");
        $list = M("users")->field('name,team,class,performance,sex,card,address,phone,email,qq,school_address,school,education,subject,computer,work_year,target,contacts_name,contacts_relation,contacts_phone')->where("class = $class")->select();
        foreach ($list as $key => $value) {
            if($value['sex'] == 0){
                $list[$key]['sex'] = 女;
            }else{
                $list[$key]['sex'] = 男;
            }

            if($value['computer'] == 0){
                $list[$key]['computer'] = 零基础;
            }elseif ($value['computer'] == 1) {
                $list[$key]['computer'] = 有一点点基础;
            }elseif ($value['computer'] == 2) {
                $list[$key]['computer'] = 计算机操作还可以;
            }elseif ($value['computer'] == 3) {
                $list[$key]['computer'] = 学过编程语言;
            }elseif ($value['computer'] == 4) {
                $list[$key]['computer'] = 有PHP编程基础;
            }elseif ($value['computer'] == 5) {
                $list[$key]['computer'] = 从事过PHP工作;
            }
            $team = M('team')->where("id = {$value['team']}")->field('name')->select();
            foreach ($team as $key1 => $value1) {
                $list[$key]['team'] = $value1['name'];
            }

            $class1 = M('class')->field('name')->where("id = {$value['class']}")->select();
            foreach ($class1 as $key2 => $value2) {
               $list[$key]['class'] = $value2['name'];
            }
            if($value['work_year'] == 0){
                $list[$key]['work_year'] = 无;
            }elseif ($value['work_year'] == 1) {
                $list[$key]['work_year'] = 一年左右;
            }elseif ($value['work_year'] == 2) {
                $list[$key]['work_year'] = 一到两年;
            }elseif ($value['work_year'] == 3) {
                $list[$key]['work_year'] = 两到三年;
            }elseif ($value['work_year'] == 4) {
                $list[$key]['work_year'] = 三到五年;
            }elseif ($value['work_year'] == 5) {
                $list[$key]['work_year'] = 五年以上;
            }
        }
       $classname=M('class')->where("id=$class")->select();
       PHPExcel(5, rtrim($classname['0']['name'],"班")."班学员详细信息", $list);
    }

    // 导出通讯录Excel表格
    public function Excel()
    {
        // 获取班级(怎么获取是get还是session你自己看下)
        $id = I("get.id");
        $classname = M('class')->where("id=$id")->select();
        $user = M('users');
        $list = $user->where("class=$id and role_id in(10,8,9)")->field('name,phone,qq,email')->select();
        $time = date("Y-m-d",time());
        PHPExcel(1, rtrim($classname['0']['name'],"班")."班通讯录".$time, $list);
    }

    // 导出保险Excel表格
    public function Excels()
    {
        // 获取班级(怎么获取是get还是session你自己看下)
        $id = I("get.id");
        $classname = M('class')->where("id=$id")->select();
        $user = M('users');
        $list = $user->where("class=$id and role_id in(10,8,9)")->field('name,card')->select();
        $time = date("Y-m-d",time());
        PHPExcel(2, rtrim($classname['0']['name'],"班")."班保险数据".$time, $list);
    }

    // 添加小组
    public function addteam(){
        $class = I("get.class");
        $this->assign('class',$class);
        $this->display("Classmanage/teams");
    }

    public function doteam(){
        $data['name'] = I('post.name');
        $data['class'] = I('post.classname');
        // var_dump($data);
        $data['addtime'] = time();
        $team = M('team');
        $info = $team->add($data);
        if($info){
            echo "添加成功";
        }else{
            echo "添加失败";
        }
    }

    // 小组成员修改
    public function tedit(){
        $date['team'] = I("get.tid");
        $data['uid'] = I("get.uid");
        echo I("get.rid");//区域角色
        echo I("get.yid");//用户角色
        if(I("get.rid") > 0){
            
            if(I("get.yid") == 8){
                $date['role_id'] = I("get.yid");
                $list = M("users")->where("id = {$data['uid']}")->save($date); 
            }else{
               $date['role_id'] = I("get.rid");
               $list = M("users")->where("id = {$data['uid']}")->save($date); 
            }
            
        }else{
            
            if(I("get.yid") == 9 or I('get.yid') == 10){
                $date['role_id'] = 10;
                $list = M("users")->where("id = {$data['uid']}")->save($date);
            }else{
               $list = M("users")->where("id = {$data['uid']}")->save($date); 
            }
        }
        if($list){
            echo '1';
        }else{
            echo '0';
        }
    }

    // 修改小组名称
    public function doedit(){
        $id = I("get.id");
        $list = M("team")->where("id=$id")->field('id,name')->select();
        // var_dump($list);
        $this->assign("list",$list);
        $this->display("Classmanage/fuck");
    }
    public function tupdate(){
        $data['id'] = I('post.id');
        $date['name'] = I('post.name');
        $list = M('team')->where($data)->save($date);
        // echo M("team")->getLastSql();
        if($list){
            echo "修改成功";
        }else{
            echo "修改失败";
        }
    }

    // 删除小组
    public function del(){
        $arr['id'] = I("post.id");
        $mod = M("team");
        $data['team'] = 0;
        $date['role_id'] = 10;
        $list = M("users")->field("role_id,team")->where("team = {$arr['id']} and role_id = 9")->save($date);
        $inff = M('users')->where("team={$arr['id']}")->save($data);
        $info = $mod->where($arr)->delete();
        if(!empty($info) or !empty($inff) or !empty($list)){
            echo "成功";
        }else{
            echo "失败";
        }
            
    }
}